package com.altuure.yablog.controller;

import com.altuure.yablog.model.Link;
import com.altuure.yablog.model.Post;
import com.altuure.yablog.model.Tag;
import com.altuure.yagdao.SearchResultList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.altuure.yablog.service.BrowserService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class BrowseController {

    protected final Log log = LogFactory.getLog(BrowseController.class);
    private BrowserService browserService = null;


    @RequestMapping("/index")
    public ModelAndView index() {
        log.debug("index");
        return browseTag(null, 0);

    }

    @RequestMapping(value = "/page/{page}", method = RequestMethod.GET)
    public ModelAndView page(@PathVariable("page") int page) {
        log.debug("page");
        return browseTag(null, page);
    }

    @RequestMapping(value = "/tag/{tag}", method = RequestMethod.GET)
    public ModelAndView browseTag(@PathVariable("tag") String tag) {
        return browseTag(tag, 0);
    }

    @RequestMapping(value = "/tag/{tag}/page/{page}", method = RequestMethod.GET)
    public ModelAndView browseTag(@PathVariable("tag") String tag, @PathVariable("page") int page) {
        return browse(tag, null, page);
    }

    @RequestMapping(value = "/category/{category}", method = RequestMethod.GET)
    public ModelAndView browseCategory(@PathVariable("category") String category) {
        return browse(category, null, 0);
    }

    @RequestMapping(value = "/category/{category}/page/{page}", method = RequestMethod.GET)
    public ModelAndView browseCategory(@PathVariable("category") String category, @PathVariable("page") int page) {
        return browse(null, category, page);
    }

    // BROWSE BY DATE
    @RequestMapping(value = "/date/{year}", method = RequestMethod.GET)
    public ModelAndView browseYear(@PathVariable("year") Integer year) {
        return browseByDate(year, null, 0);
    }


    @RequestMapping(value = "/date/{year}", method = RequestMethod.GET)
    public ModelAndView browseYear(@PathVariable("year") Integer year, @PathVariable("page") Integer page) {
        return browseByDate(year, null, page);
    }

    @RequestMapping(value = "/date/{year}/{month}", method = RequestMethod.GET)
    public ModelAndView browseMonth(@PathVariable("year") Integer year, @PathVariable("month") Integer month) {
        return browseByDate(year, month, 0);
    }

    @RequestMapping(value = "/date/{year}/{month}/page/{page}", method = RequestMethod.GET)
    public ModelAndView browseMonth(@PathVariable("year") Integer year, @PathVariable("month") Integer month, @PathVariable("page") Integer page) {
        return browseByDate(year, month, page);
    }

    // search
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public ModelAndView browseYear(@RequestParam("q") String keyword) {
        return browseSearch(keyword);
    }
     @RequestMapping(value = "/post/{id}", method = RequestMethod.GET)
    public ModelAndView browseYear(@PathVariable("id") long postId) {
        return browsePost(postId);
    }
    private ModelAndView browsePost( long postId) {
        log.debug("browsePost");
        ModelAndView modelAndView = new ModelAndView("browse");
        Map<String, Object> map = modelAndView.getModel();
        ArrayList<Post> posts=new ArrayList();
        posts.add(browserService.getPost(postId));
        map.put("posts", posts);
        map.put("pageCount",0);
        map.put("pageIndex", 0);
        return pageTemplate(modelAndView, map);
    }

    private ModelAndView browseSearch(String keyword) {
        log.debug("browseSearch");
        ModelAndView modelAndView = new ModelAndView("browse");
        Map<String, Object> map = modelAndView.getModel();
        SearchResultList<Post> posts = browserService.search(keyword);
        map.put("posts", posts);
        map.put("pageCount", posts.getPageCount());
        map.put("pageIndex", posts.getPageIndex());
        return pageTemplate(modelAndView, map);
    }

    private ModelAndView browseByDate(Integer year, Integer month, int page) {
        log.debug("browseByDate");
        ModelAndView modelAndView = new ModelAndView("browse");
        Map<String, Object> map = modelAndView.getModel();
        SearchResultList<Post> posts = browserService.getPostListByDate(year, month, page);
        map.put("posts", posts);
        map.put("pageCount", posts.getPageCount());
        map.put("pageIndex", posts.getPageIndex());

        return pageTemplate(modelAndView, map);
    }

    public ModelAndView browse(String tag, String category, int page) {
        log.debug("browseTag");
        ModelAndView modelAndView = new ModelAndView("browse");
        Map<String, Object> map = modelAndView.getModel();
        SearchResultList<Post> posts = browserService.getPostList(page, tag, category);
        map.put("posts", posts);
        int pageCount = browserService.getPageCount();
        map.put("pageCount", posts.getPageCount());
        map.put("pageIndex", posts.getPageIndex());

        return pageTemplate(modelAndView, map);
    }

    private ModelAndView pageTemplate(ModelAndView modelAndView, Map<String, Object> map) {
        List<Post> listByDate = browserService.listbyDate();

        List<Link> links = browserService.getLinks();
        List popularTags = browserService.popularTags();
        List categories = browserService.getCategories();
        List<Tag> tags = browserService.popularTags();


        map.put("listByDate", listByDate);
        map.put("links", links);
        map.put("tags", tags);
        map.put("popularTags", popularTags);
        map.put("categories", categories);


        return modelAndView;
    }


    @Autowired
    public void setBrowserService(BrowserService browserService) {
        this.browserService = browserService;
    }
}
